package com.tc.zigbee.mt.income;

import com.tc.util.DataUtil;
import com.tc.zigbee.zcl.ZclMessageParser;
import com.tc.zigbee.zcl.ZclRawCommand;
import com.tc.zigbee.zcl.command.AbstractZclCommand;

public class AfIncomeMessage extends IncomeMessage{
	public short getGroupId() {
		return groupId;
	}
	public short getClusterId() {
		return clusterId;
	}
	public short getSrcAddr() {
		return srcAddr;
	}
	public byte getSrcEndpoint() {
		return srcEndpoint;
	}
	public byte getDstEndpoint() {
		return dstEndpoint;
	}
	public byte getWasBroadcast() {
		return wasBroadcast;
	}
	public byte getLen() {
		return len;
	}
	public ZclRawCommand getZclCommand() {
		return zclCommand;
	}
	public AbstractZclCommand getParsedCommand() {
		return parsedCommand;
	}
	public AfIncomeMessage(byte[] data){

		byte[] na = new byte[2];
		na[0] = data[0];
		na[1] = data[1];
		groupId = DataUtil.shortValue(na);
		na[0] = data[2];
		na[1] = data[3];
		clusterId = DataUtil.shortValue(na);
		na[0] = data[4];
		na[1] = data[5];
		srcAddr = DataUtil.shortValue(na);
		srcEndpoint = data[6];
		dstEndpoint = data[7];
		wasBroadcast = data[8];
		linkquality = data[9];

		securityUse = data[10];
		byte[] t = new byte[4];
		t[0] = data[11];
		t[1] = data[12];
		t[2] = data[13];
		t[3] = data[14];
		timestamp = DataUtil.intValue(t);
		transSecNumber = data[15];
		len = data[16];
		pdata = new byte[len];
		for(int i = 0; i < len; i++){
			pdata[i] = data[17 + i];
		}
		zcl();
	}
	private void zcl() {
//		0x0000 – 0x7fff Standard ZigBee application profile.
//		0x8000 – 0xbfff Reserved.
//		0xc000 – 0xffff Manufacturer Specific application profile.
		int cid = this.clusterId;
		if(cid >= 0x0000 && cid < 0x7ffff){
			this.zclCommand = new ZclRawCommand(this.pdata);

			this.parsedCommand = ZclMessageParser.parse(this.clusterId, zclCommand.getFrameControl(), zclCommand.getCommandIdentifier(), zclCommand.getPayload());
		}

	}
	short groupId;
	short clusterId;
	short srcAddr;
	byte srcEndpoint;
	byte dstEndpoint;
	byte wasBroadcast;
	byte linkquality;
	byte securityUse;
	int timestamp;
	byte transSecNumber;
	byte len;
	byte[] pdata;
	ZclRawCommand zclCommand;
	AbstractZclCommand parsedCommand;

//	Length = 0x11-0x91  Cmd0 = 0x44  Cmd1 = 0x81  GroupId  ClusterId  SrcAddr  SrcEndpoint 
//	1  1  1  1  4  1  1  0-128 
//	DstEndpoint  WasBroadcast  LinkQuality  SecurityUse  Timestamp  TransSeqNumber  Len  Data 
}
